package com.huidu.bitrade.hdwallet;

import org.apache.commons.codec.digest.DigestUtils;

import java.util.Random;

public class SignUtil {

    public static boolean checkSign(String key , String timestamp,String nonce,String body,String sign) {
        String checkSign = sign(key,timestamp,nonce,body);
        return checkSign.equals(sign);
    }

    public static String sign(String key,String timestamp,String nonce,String body) {
        String raw = body + key + nonce + timestamp;
        return DigestUtils.md5Hex(raw).toLowerCase();
    }

    public static String getNonceString(int len){
        String seed = "1234567890";
        StringBuilder tmp = new StringBuilder();
        for (int i = 0; i < len; i++) {
            tmp.append(seed.charAt(getRandomNumber(0,9)));
        }
        return tmp.toString();
    }

    public static int getRandomNumber(int from, int to) {
        float a = from + (to - from) * (new Random().nextFloat());
        int b = (int) a;
        return ((a - b) > 0.5 ? 1 : 0) + b;
    }
}
